package roles

import (
	"xagent/internal/actors"
	"xagent/internal/knowledge"
	"xagent/internal/prompts"
	proto "xagent/proto/v1"
)

// DomainExpert represents a domain expert with specialized knowledge
type DomainExpert struct {
	*actors.BaseAgent
	templates *prompts.TemplateManager
	rag       *knowledge.RAGEngine
	domain    string
	expertise []string
}

// NewDomainExpert creates a new domain expert agent
func NewDomainExpert() *DomainExpert {
	baseAgent := actors.NewBaseAgent(
		"domain_expert",
		"Domain knowledge and business requirements expert",
	)
	return &DomainExpert{
		BaseAgent: baseAgent,
		templates: prompts.NewTemplateManager(),
		rag:       nil,
		domain:    "",
		expertise: []string{},
	}
}

// ProcessTask handles domain expert tasks
func (d *DomainExpert) ProcessTask(task *proto.Task) {
	switch task.Name {
	case "ProvideInsight":
		d.handleProvideInsight(task)
	case "ValidateDesign":
		d.handleValidateDesign(task)
	case "ReviewImplementation":
		d.handleReviewImplementation(task)
	}
}

// handleProvideInsight handles insight provision tasks
func (d *DomainExpert) handleProvideInsight(task *proto.Task) {
	response := &proto.AgentMessage{
		Id:   task.Id,
		Type: proto.MessageType_MESSAGE_TYPE_RESPONSE,
		Content: &proto.AgentMessage_Message{
			Message: "Domain insights provided",
		},
		Metadata: map[string]string{
			"type":   "domain_insight",
			"status": "completed",
		},
	}

	if parent := d.GetContext().Parent(); parent != nil {
		d.GetContext().Send(parent, response)
	}
}

// handleValidateDesign handles design validation tasks
func (d *DomainExpert) handleValidateDesign(task *proto.Task) {
	response := &proto.AgentMessage{
		Id:   task.Id,
		Type: proto.MessageType_MESSAGE_TYPE_RESPONSE,
		Content: &proto.AgentMessage_Message{
			Message: "Design validation completed",
		},
		Metadata: map[string]string{
			"type":   "design_validation",
			"status": "completed",
		},
	}

	if parent := d.GetContext().Parent(); parent != nil {
		d.GetContext().Send(parent, response)
	}
}

// handleReviewImplementation handles implementation review tasks
func (d *DomainExpert) handleReviewImplementation(task *proto.Task) {
	response := &proto.AgentMessage{
		Id:   task.Id,
		Type: proto.MessageType_MESSAGE_TYPE_RESPONSE,
		Content: &proto.AgentMessage_Message{
			Message: "Implementation review completed",
		},
		Metadata: map[string]string{
			"type":   "implementation_review",
			"status": "completed",
		},
	}

	if parent := d.GetContext().Parent(); parent != nil {
		d.GetContext().Send(parent, response)
	}
}
